package com.xywy.bloodandroid.act;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.HttpUtil;
import com.xywy.bloodandroid.util.HttpUtil.ResponseCallBack;
import com.xywy.bloodandroid.util.MD5;

/**
 * 修改手机号 <br>
 * 验证手机号？
 */
public class ChanePhoneNumberActivity extends BaseActivity {

	private TextView middle_title; // title
	private RelativeLayout left_img; // left click
	private TextView tv_tip; // tip
	private EditText et_phone_number; // get phone number
	private ImageView btn_clear_inputNum; // clear et_phone_number
	private Button btn_change_phone_number; // commit change phone

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_change_phone_number);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("验证手机号");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		tv_tip = (TextView) findViewById(R.id.tv_tip);
		et_phone_number = (EditText) findViewById(R.id.et_phone_number);
		btn_clear_inputNum = (ImageView) findViewById(R.id.btn_clear_inputNum);
		btn_change_phone_number = (Button) findViewById(R.id.btn_change_phone_number);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		btn_clear_inputNum.setOnClickListener(this);
		btn_change_phone_number.setOnClickListener(this);
		btn_change_phone_number.setClickable(false);
		btn_change_phone_number
				.setBackgroundResource(R.drawable.login_button_press);
		et_phone_number.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (et_phone_number.getText().toString().length() > 0) {
					btn_clear_inputNum.setVisibility(View.VISIBLE);
				}
				if (et_phone_number.getText().toString().length() == 11) {
					btn_change_phone_number.setClickable(true);
					btn_change_phone_number
							.setBackgroundResource(R.drawable.login_button_selector);
				} else {
					btn_change_phone_number.setClickable(false);
					btn_change_phone_number
							.setBackgroundResource(R.drawable.login_button_press);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
	}

	@Override
	protected void initData() {
		/* 电话 */
		String phoneNum = User.getInstance(this).getPhonenum();
		if (phoneNum.length() != 11) {
			phoneNum = "13812345678";
		}
		tv_tip.setText("更改成功后，下次登录可以使用新的手机号码，当前手机号码为：" + phoneNum);

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			ChanePhoneNumberActivity.this.finish();
			break;

		case R.id.btn_clear_inputNum:
			et_phone_number.setText("");
			break;
		case R.id.btn_change_phone_number:
			if (et_phone_number.getText().toString().trim().length() == 11) {
				changePhoneNum();
			} else
				showToast("手机号码不正确");
			break;
		}
	}

	/**
	 * &target=13104000200 （目标手机号） &type=1 or 2 or 3 注：type 1.短信登录前的验证提示。
	 * 2.改密码前的验证提示。 3.更改绑定手机前的提示。
	 */
	void changePhoneNum() {
		String sign = null;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String url = "http://api.wws.xywy.com/index.php?act=sms&fun=sendCode&target="
				+ et_phone_number.getText().toString().trim()
				+ "&tag="
				+ Constants.tag + "&sign=" + sign + "&type=3";
		System.out.println("======="+url);
		HttpUtil.httpclientDoGet(url, new ResponseCallBack() {

			private String id;

			@Override
			public void success(String result) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonObject = new JSONObject(result);
					int code = jsonObject.getInt("code");
					 id = jsonObject.getString("id");
					if (code == 10000) {
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								Intent intent = new Intent(
										ChanePhoneNumberActivity.this,
										ComfirmChangePhoneNumberActivity.class);
								intent.putExtra("new_phone_num",
										et_phone_number.getText().toString()
												.trim());
								intent.putExtra("id", id);
								showToast("短信发送中");
								startActivity(intent);
								ChanePhoneNumberActivity.this.finish();
							}
						});
					} else {
						showToast(jsonObject.getString("error"));
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void error(Exception e) {
				// TODO Auto-generated method stub

			}
		});
		//
		// GetRequest<String> getRequest = new GetRequest<String>(url,
		// String.class, new OnVolleyResponseListener<String>() {
		//
		// @Override
		// public void onError(VolleyError volleyError) {
		// showToast(volleyError.toString());
		// }
		//
		// @Override
		// public void onSuccess(String response) {
		// try {
		// JSONObject jsonObject = new JSONObject(response);
		// int code = jsonObject.getInt("code");
		// if (code == 10000) {
		// Intent intent = new Intent(
		// ChanePhoneNumberActivity.this,
		// ComfirmChangePhoneNumberActivity.class);
		// intent.putExtra("new_phone_num",
		// et_phone_number.getText().toString()
		// .trim());
		// showToast("短信发送中");
		// startActivity(intent);
		// } else {
		// showToast(jsonObject.getString("error"));
		// }
		//
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }
		//
		// }
		//
		// });
		//
		// getRequest.setIsParseJson(false);
		// VolleyManager.addRequest(getRequest, this);

	}
}
